package com.smart.speaker.websocket.tool;

import com.alibaba.dashscope.tools.FunctionDefinition;
import com.alibaba.dashscope.tools.ToolFunction;
import com.google.gson.JsonObject;
import org.springframework.stereotype.Component;

import java.time.LocalTime;

@Component(value = "get_current_time")
public class GetTimeTool implements Tool {

    private final ToolFunction function;

    public GetTimeTool() {
        JsonObject parameters = new JsonObject();
        parameters.addProperty("type", "object");
        function = ToolFunction.builder().function(FunctionDefinition.builder().name("get_current_time")
                .description("获取当前时刻的时间")
                .parameters(parameters).build()).build();
    }

    @Override
    public ToolResp call(String params) {
        LocalTime now = LocalTime.now();
        int hour = now.getHour();
        int min = now.getMinute();
        int sec = now.getSecond();
        String content;
        if (hour <= 12) {
            content = "现在是上午" + hour + "点" + min + "分" + sec + "秒";
        } else if (hour <= 18) {
            content = "现在是下午" + (hour - 12) + "点" + min + "分" + sec + "秒";
        } else {
            content = "现在是晚上" + (hour - 12) + "点" + min + "分" + sec + "秒";
        }
        return new ToolResp(Type.RESPONSE, content);
    }

    @Override
    public ToolFunction getFunction() {
        return function;
    }

}
